package com.darkplanet.management.dao;

import com.darkplanet.management.domain.Employee;
import com.darkplanet.management.util.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class EmployeeDaoImp implements IEmployeeDao{
    QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDs());

    @Override
    public Employee getEmployeeById(Integer id) {
        String sql = "select * from employee where employee_id = ?";
        Employee employee = null;
        try {
            employee = queryRunner.query(sql, new BeanHandler<Employee>(Employee.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return employee;
    }

    @Override
    public List<Employee> getEmployees() {
        String sql = "select * from employee";
        List<Employee> employees = null;
        try {
            employees = queryRunner.query(sql, new BeanListHandler<Employee>(Employee.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return employees;
    }

    @Override
    public void addEmployee(String position, String e_name, String gender, Integer age, String phone, String work_date, String id_card, String remark) {
        String sql = "insert into employee (position,e_name,e_pwd,gender,age,phone,work_date,id_card,remark) values(?,?,?,?,?,?,?,?,?)";
        try {
            queryRunner.update(sql,position,e_name,"123456",gender,age,phone,work_date,id_card,remark);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteEmployeeById(Integer id) {
        String sql = "delete from employee where employee_id = ?";
        try {
            queryRunner.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateEmployeeById(String position, String e_name, String gender, Integer age, String phone, String work_date, String id_card, String remark,Integer id) {
        String sql = "update employee set position=?,e_name=?,gender=?,age=?,phone=?,work_date=?,id_card=?,remark=? where employee_id = ?";
        try {
            queryRunner.update(sql,position,e_name,gender,age,phone,work_date,id_card,remark,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateEmployeePwd(String pwd, Integer id) {
        String sql = "update employee set e_pwd = ? where employee_id = ?";
        try {
            queryRunner.update(sql,pwd,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Employee getEmployeeBYNamePwd(String e_name,String e_pwd) {
        String sql = "select * from employee where e_name = ? and e_pwd = ?";
        Employee employee = null;
        try {
            employee = queryRunner.query(sql, new BeanHandler<Employee>(Employee.class),e_name,e_pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return employee;
    }
}
